/*
    2D FDTD simulator
    Copyright (C) 2019 Emilia Blåsten

    This program is free software: you can redistribute it and/or
    modify it under the terms of the GNU Affero General Public License
    as published by the Free Software Foundation, either version 3 of
    the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public
    License along with this program.  If not, see
    <http://www.gnu.org/licenses/>.
*/
/* updatetmz.c: Functions to update the fields. Depending on the type
 * of grid, the fields can be treated as either one-.or two-dimensional. */

#include "gridtmz.h"
#include "grid1dez.h"
#include "updatetmz.h"
#include <stdio.h>

// update 1D magnetic field
void updateH1d(Grid1D *g) {
  int mm;
  for(mm = 0; mm < g->sizeX - 1; mm++) {
    g->hy[mm] = g->hyUpdateHcoeff[mm] * g->hy[mm]
      + g->hyUpdateEcoeff[mm] * (g->ez[mm+1]
      - g->ez[mm]);
  }

  return;
}

// update magnetic field
void updateH2d(Grid *g) {
  int mm, nn;

  for(mm = 0; mm < g->sizeX; mm++) {
    for(nn = 0; nn < g->sizeY - 1; nn++) {
      g->hx[mm][nn] = g->hxUpdateHcoeff[mm][nn] * g->hx[mm][nn]
        - g->hxUpdateEcoeff[mm][nn] * (g->ez[mm][nn+1]
        - g->ez[mm][nn]);
    }
  }

  for(mm = 0; mm < g->sizeX - 1; mm++) {
    for(nn = 0; nn < g->sizeY; nn++) {
      g->hy[mm][nn] = g->hyUpdateHcoeff[mm][nn] * g->hy[mm][nn]
        + g->hyUpdateEcoeff[mm][nn] * (g->ez[mm+1][nn]
        - g->ez[mm][nn]);
    }
  }

  return;
}

// update 1D electric field
void updateE1d(Grid1D *g) {
  int mm;
  for(mm = 1; mm < g->sizeX - 1; mm++) {
    g->ez[mm] = g->ezUpdateEcoeff[mm] * g->ez[mm]
      + g->ezUpdateHcoeff[mm] * (g->hy[mm] - g->hy[mm-1]);
  }

  return;
}
// update electric field
void updateE2d(Grid *g) {
  int mm, nn;

  for(mm = 1; mm < g->sizeX - 1; mm++) {
    for(nn = 1; nn < g->sizeY - 1; nn++) {
      g->ez[mm][nn] = g->ezUpdateEcoeff[mm][nn] * g->ez[mm][nn]
        + g->ezUpdateHcoeff[mm][nn] * (
          (g->hy[mm][nn] - g->hy[mm-1][nn])
          - (g->hx[mm][nn] - g->hx[mm][nn-1])
        );
    }
  }

  return;
}

